/******************************************************************
 * 文件：main.c
 * 功能：主函数入口
 * 日期：2018-02-16
 * 作者：zx
 * 版本：Ver.1.0 | 最初版本
 * 官博：http://fengmeitech.club
 * Copyright (C) 2017 zx. All rights reserved.
*******************************************************************/
#include "stdio.h"
#include "string.h"
#include "DELAY/Delay.h"
#include "ADC/ADC.h"
#include "LED/LED.h"
#include "NVIC/NVIC.h"
#include "IIC/IIC.h"
#include "OLED/OLED.h"
#include "RELAY/Relay.h"
#include "DHT11/DHT11.h"
#include "UART/uart.h"
#include "misc.h"
#include "TIMx/TIMx.h"
#include "EXTI/EXTI.h"
#include "PID/PID.h"
#include "DATA/DATA.h"
#include "NEC/NEC.h"
#include "NetWork.h"
#include "TLink.h"
#include "PID/PID.h"

//InitSingleFDC2214();//初始化单通道
#include "FDC2214/bsp_fdc2214.h"
#include "sys.h"
#include "F_I2C/bsp_iic.h"


//#include "SERVER/Server.h"
//#include "HX711/HX711.h"
//#include "MPU6050/UART_MPU6050.h"



void TimerInit(void);
void TIM3_IRQHandler(void);
unsigned long G=0;



DeviceSta_Strcture device={0};
int ChValue[4] = {0}; 





u16 DHT11_data; //存储DHT11传感器采集数据
int main(void)
{
    //u8 i = 0;


	/*初始化各外设*/ 
		initSysTick(); 
		Delay_ms(1000);
	  
    //initADC();
    //initUART2();
    //initUART3();
	
    initUART();
		Delay_ms(5);
		IIC_Init();//初始化i2c用于FDC2214  引脚为B10 和 B11
		Delay_ms(5);
		IIC_Init();//初始化i2c用于FDC2214  引脚为B10 和 B11
		Delay_ms(5);
		IIC_Init();//初始化i2c用于FDC2214  引脚为B10 和 B11
		Delay_ms(5);
		IIC_Init();//初始化i2c用于FDC2214  引脚为B10 和 B11
		Delay_ms(5);
    InitMultiFDC2214(); //初始化FDC2214
		Delay_ms(5);
		InitMultiFDC2214(); //初始化FDC2214
		Delay_ms(5);
		InitMultiFDC2214(); //初始化FDC2214
		Delay_ms(5);
   
		
		initLED();
    initKey();
    initEXTI_0_1_2();
    initNVIC(NVIC_PriorityGroup_2); //设置NVIC分组以及对应外设中断优先级
    
    //initDHT11();


		initIIC();   //初始化i2c用于OLED 引脚为B0 和 B1
    initOLED();

    initRelay();  //蜂鸣器

    
		Delay_ms(100);
	//initNVIC(NVIC_PriorityGroup_2);//开启UART2的接收和空闲中断
	//initTIMx(TIM3,7199,19999,TIM_IT_Update,ENABLE);
	//TIM_ARRPreloadConfig(TIM3,ENABLE);
		formatScreen(0x00);//清屏
		showString(0,1,"welcome!",FONT_16_EN);
		Is_Test.interval=1;
    while(1)
    {
       //pid.Now_place=getval();//getval()泛指读取函数
		//ChValue[0] = FDC2X14ReadCH(1); 
		ChValue[1] = FDC2X14ReadCH(3); 
		ChValue[2] = Testing(); 
		if(ChValue[2] > MAX)
		{
			formatScreen(0x00);//清屏
			showString(0,1,"Error:",FONT_16_EN);
			showString(0,3,"short circuit!",FONT_16_EN);
			Buzzing(10, 50);
		}
		//ChValue[3] = FDC2X14ReadCH(4); 
		printf("%d,",ChValue[0]);
		printf("%d,",ChValue[1]);
		printf("%d,",ChValue[2]);
		printf("%d\n",ChValue[3]);
		//showNumber(40,3,ChValue[2],DEC,3,FONT_16_EN);
		Delay_ms(2);	
       
       

			
    }

}
void TIM3_IRQHandler(void)
{
  
    
    
     
        if(TIM_GetITStatus(TIM3, TIM_IT_Update) == SET)
        {
            // TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
            // DHT11_data = readDHT11();
            // device.Humidity = DHT11_data>>8;
            // device.Temperature = DHT11_data&0x00FF;
            // device.Lux = getConvValueAve(10,1000);
            // sendDeviceStatus(&device);
            // toggleLED();
            PID_OUT();
        }
    
}

    
    







    
	
